package com.ww.springboot.boot.algorithm.leetcode1;

import java.util.*;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-03-11 09:16
 */
public class AG49字母异位词分组 {


    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        System.out.println(groupAnagrams(strs));

        //System.out.println(stringSort("CFGSXAZ"));
    }

    public static List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            String ss = stringSort(strs[i]);
            //学习--写法
            List<String> list = map.getOrDefault(ss, new ArrayList<String>());
            list.add(strs[i]);
        }
        //学习--写法
        return new ArrayList<>(map.values());
    }

    private static String stringSort(String ss) {
        char[] chars = ss.toCharArray();
        Arrays.sort(chars);
        return new String(chars);
    }
}
